import collections

from coin.exchange.okex_futures.kr_rest.futures_product import OkexFuturesProduct
from coin.exchange.huobi_futures.kr_rest.futures_product import HuobiFuturesProduct
from coin.exchange.bitmex.kr_rest.futures_product import BitmexFuturesProduct
from coin.exchange.kraken_futures.kr_rest.futures_product import KrakenFuturesProduct
from coin.exchange.kraken.kr_rest.product import KrakenProduct

Logspec = collections.namedtuple('Logspec', ['bases', 'path', 'marker', 'name', 'group'])

okex_v1feed_quarter = Logspec(
    ["btc", "eth"],
    '~/data/log/server-02.aliyun-cn-hongkong.okex/log_lmagg_okex_quarter_agg',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter',
    'lm-agg.okex_%s_fut')

okex_quarter_self = Logspec(
    ["btc", "eth", "eos"],
    '~/data/log/server-02.aliyun-cn-hongkong.okex/log_lmagg_okex_quarter_agg_self',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter_self',
    'lm-agg.okex_%s_fut')

okex_v1feed_quarter2 = Logspec(
    ["eos", "ltc"],
    '~/data/log/server-03.aliyun-cn-hongkong.okex/log_lmagg_okex_quarter_agg2',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter2',
    'lm-agg.okex_%s_fut')

okex_v3feed_quarter = Logspec(
    ["btc", "eth", "eos"],
    '~/data/log/server-01.aliyun-cn-hongkong.okex/log_okex_lm_agg_quarter_v3feed',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter.v3feed',
    'lm-agg.okex_%s_fut')

okex_v3feed_thisweek = Logspec(
    ["btc", "eth", "eos"],
    '~/data/log/server-01.aliyun-cn-hongkong.okex/log_okex_lm_agg_this_week_v3feed',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
    'lm-agg.okex_%s_thisweek.v3feed',
    'lm-agg.okex_%s_fut')

okex_v3feed_quarter2 = Logspec(
    ["bch", "ltc"],
    '~/data/log/server-03.aliyun-cn-hongkong.okex/log_okex_lm_agg_quarter_v3feed_2',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter.v3feed_2',
    'lm-agg.okex_%s_fut')

okex_thisweek = Logspec(["btc", "eth"],
                        '~/data/log/server-02.aliyun-cn-hongkong.okex/log_lmagg_okex_thisweek',
                        lambda base: OkexFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
                        'lm-agg.okex_%s_thisweek',
                        'lm-agg.okex_%s_fut')

okex_thisweek2 = Logspec(["eos", "ltc"],
                         '~/data/log/server-03.aliyun-cn-hongkong.okex/log_lmagg_okex_thisweek2',
                         lambda base: OkexFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
                         'lm-agg.okex_%s_thisweek2',
                         'lm-agg.okex_%s_fut')

okex_swap = Logspec(["btc", "eth", "eos", "ltc"],
                    '~/data/log/server-02.aliyun-cn-hongkong.okex/log_lmagg_okex_swap',
                    lambda base: OkexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                    'lm-agg.okex_%s_swap',
                    'lm-agg.okex_%s_fut')

huobi_quarter_self = Logspec(
    ['btc', 'eth', 'eos', 'ltc', 'bch'],
    '~/data/log/strategy-16.ap-northeast-1.huobi/log_lmagg_huobi_quarter_self',
    lambda base: HuobiFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.huobi_%s_quarter_self',
    'lm-agg.huobi_%s_fut',
)

huobi_quarter = Logspec(
    ['btc', 'eth', 'eos'],
    '~/data/log/strategy-11.ap-northeast-1.huobi/log_lmagg_huobi_quarter',
    lambda base: HuobiFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.huobi_%s_quarter',
    'lm-agg.huobi_%s_fut',
)

huobi_quarter2 = Logspec(
    ['ltc', 'bch'],
    '~/data/log/strategy-16.ap-northeast-1.huobi/log_lmagg_huobi_quarter2',
    lambda base: HuobiFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.huobi_%s_quarter2',
    'lm-agg.huobi_%s_fut',
)

huobi_thisweek = Logspec(
    ['btc', 'eth', 'eos'],
    '~/data/log/strategy-10.ap-northeast-1.huobi/log_lmagg_huobi_this_week',
    lambda base: HuobiFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
    'lm-agg.huobi_%s_thisweek',
    'lm-agg.huobi_%s_fut',
)

huobi_thisweek2 = Logspec(
    ['ltc', 'bch'],
    '~/data/log/strategy-16.ap-northeast-1.huobi/log_lmagg_huobi_this_week2',
    lambda base: HuobiFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
    'lm-agg.huobi_%s_thisweek2',
    'lm-agg.huobi_%s_fut',
)

okex_jab_quarter = Logspec(
    ["bch", "xrp"],
    '~/data/log/server-03.aliyun-cn-hongkong.okex/log_lmagg_okex_quarter_jab_agg',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.QUARTER" % base),
    'lm-agg.okex_%s_quarter_jab',
    'lm-agg.okex_%s_fut')

okex_jab_this_week = Logspec(
    ["bch", "xrp"],
    '~/data/log/server-03.aliyun-cn-hongkong.okex/log_lmagg_okex_thisweek_jab',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.THIS_WEEK" % base),
    'lm-agg.okex_%s_thisweek_jab',
    'lm-agg.okex_%s_fut')

okex_jab_swap = Logspec(["bch"],
                        '~/data/log/server-03.aliyun-cn-hongkong.okex/log_lmagg_okex_swap_jab_agg',
                        lambda base: OkexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                        'lm-agg.okex_%s_swap_jab',
                        'lm-agg.okex_%s_fut')

bitmex_xbt = Logspec(["btc"],
                     '~/data/log/strategy-15.eu-west-1/log_bitmex-xbt.lmagg',
                     lambda base: BitmexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                     'lm-agg.bitmex_%susd',
                     'lm-agg.bitmex_%s')

bitmex_xbt2 = Logspec(["btc"],
                      '~/data/log/strategy-15.eu-west-1/log_bitmex-xbt-5s.lmagg',
                      lambda base: BitmexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                      'lm-agg.bitmex_%susd2',
                      'lm-agg.bitmex_%s')

bitmex_eth = Logspec(["eth"],
                     '~/data/log/strategy-15.eu-west-1/log_bitmex-eth.lmagg',
                     lambda base: BitmexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                     'lm-agg.bitmex_%susd',
                     'lm-agg.bitmex_%s')

kraken_xbt = Logspec(["btc"],
                     '~/data/log/strategy-08.us-east-2/log_kraken_swap',
                     lambda base: KrakenFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                     'lm-agg.kraken_%s_swap',
                     'lm-agg.kraken_%s')

kraken_usd = Logspec(["usd"],
                     '~/data/log/strategy-08.us-east-2/log_kraken_usd',
                     lambda base: base,
                     'lm-agg.kraken_btc_eth_%s',
                     'lm-agg.kraken_%s')

kraken_eur = Logspec(["eur"],
                     '~/data/log/strategy-08.us-east-2/log_kraken_eur',
                     lambda base: base,
                     'lm-agg.kraken_btc_eth_%s',
                     'lm-agg.kraken_%s')

bitstamp_usd = Logspec(["usd"],
                       '~/data/log/strategy-08.eu-central-1/log_bitstamp_usdt.lmagg',
                       lambda base: base,
                       'lm-agg.bitstamp_%s',
                       'lm-agg.bitstamp_%s')

coinone1 = Logspec(["krw"],
                   '~/data/log/strategy-34.ap-northeast-2/log_coinone_lm_btc_eth',
                   lambda base: base,
                   'lm-agg.coinone_%s1',
                   'lm-agg.coinone_%s1')

coinone2 = Logspec(["krw"],
                   '~/data/log/strategy-34.ap-northeast-2/log_coinone_lm_bch_eos_xrp',
                   lambda base: base,
                   'lm-agg.coinone_%s2',
                   'lm-agg.coinone_%s2')

pass_okex_swap = Logspec(["btc"],
                         '~/data/log/strategy-23.aliyun-cn-hongkong/log_pass-okex-btc-swap.lmagg',
                         lambda base: OkexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
                         'lm-pass.okex_%s_swap',
                         'lm-pass.okex_%s_fut')

pass_okex_swap2 = Logspec(
    ["btc"],
    '~/data/log/strategy-23.aliyun-cn-hongkong/log_pass-okex-btc-swap-5s.lmagg',
    lambda base: OkexFuturesProduct.FromStr("%s-USD.PERPETUAL" % base),
    'lm-pass.okex_%s_swap_5s',
    'lm-pass.okex_%s_fut')

LMAGG_FUTS = []

for logspec in [
    pass_okex_swap,
    pass_okex_swap2,
    okex_quarter_self,
    okex_v1feed_quarter,
    okex_v1feed_quarter2,
    okex_v3feed_quarter,
    okex_v3feed_thisweek,
    okex_v3feed_quarter2,
    okex_thisweek,
    okex_thisweek2,
    okex_swap,
    okex_jab_quarter,
    okex_jab_this_week,
    okex_jab_swap,
    huobi_quarter2,
    huobi_quarter,
    huobi_quarter_self,
    huobi_thisweek,
    huobi_thisweek2,
    bitmex_xbt,
    bitmex_xbt2,
    bitmex_eth,
    kraken_xbt,
    kraken_usd,
    kraken_eur,
    bitstamp_usd,
    coinone1,
    coinone2
]:
  for base in logspec.bases:
    ubase = base.upper()
    if ubase == 'BCH':
      ubase = 'BCHABC'
    quote = ubase
    quote2 = quote
    if logspec.name.find("bitmex") >= 0:
      quote = "BTC"
      quote2 = "BTC2"
    fill_collector = 'csv'
    spot_mode = False
    balance_marker = 'Total%s' % quote
    position_marker = logspec.marker(ubase)
    logspec_name = logspec.name % base

    if logspec_name.find("coinone") >= 0:
      if logspec_name.find("1") >= 0:
        ubase = "BTC"
      else:
        ubase = "BCHABC"
      position_marker = 'KRW'
      balance_marker = 'Total'
      quote = "KRW"
      quote2 = "KRW"
      spot_mode = True
    if logspec_name.find("kraken") >= 0 or logspec_name.find("bitstamp") >= 0:
      if logspec_name.find("swap") >= 0:
        quote = "BTC"
        quote2 = "BTC2"
      else:
        balance_marker = 'Total'
        spot_mode = True
        if logspec_name.find("usd") >= 0:
          position_marker = 'USD'
          quote = "USD"
          quote2 = "USD"
        else:
          position_marker = 'EUR'
          quote = "EUR"
          quote2 = "EUR"
    LMAGG_FUTS.append({
        'owner': 'jhkim',
        'name': logspec_name,
        'group': logspec.group % base,
        'log_path': logspec.path,
        'fill_collector': fill_collector,
        'balance_marker': balance_marker,
        'position_marker': position_marker,
        'quote': quote2,
        'spot_mode': spot_mode,
    })
